package cn.com.thingo.hyintelligent.presenter;

import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZAccessToken;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import cn.com.thingo.hyintelligent.base.RxPresenter;
import cn.com.thingo.hyintelligent.base.contract.EzDeviceManageContract;
import cn.com.thingo.hyintelligent.model.DataManager;
import cn.com.thingo.hyintelligent.model.bean.BaseBean;
import cn.com.thingo.hyintelligent.model.bean.EzAddDevicePermissionBean;
import cn.com.thingo.hyintelligent.model.bean.EzDeviceAuthorizationListBean;
import cn.com.thingo.hyintelligent.util.LogUtil;
import cn.com.thingo.hyintelligent.util.RxUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class EzDeviceManagerPresenter extends RxPresenter<EzDeviceManageContract.View> implements EzDeviceManageContract.Presenter {
    private DataManager dataManager;

    @Inject
    public EzDeviceManagerPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void getDeviceInfoList() {

        Observable.create(new ObservableOnSubscribe<List<EZDeviceInfo>>() {
            @Override
            public void subscribe(ObservableEmitter<List<EZDeviceInfo>> e) throws Exception {
                EZAccessToken ezAccessToken = EZOpenSDK.getInstance().getEZAccessToken();
                if (ezAccessToken != null) {
                    List<EZDeviceInfo> infos = EZOpenSDK.getInstance().getDeviceList(0, 20);
                    e.onNext(infos);
                } else {
                    e.onNext(null);
                }
            }
        }).subscribeOn(Schedulers.newThread()).observeOn(
                AndroidSchedulers.mainThread()
        ).subscribe(new Consumer<List<EZDeviceInfo>>() {
            @Override
            public void accept(List<EZDeviceInfo> deviceInfos) throws Exception {
                mView.showDeviceInfoList(deviceInfos);
            }

        });
    }

    @Override
    public void addEzDevicePermission(String accessToken, String tel, String toTel, String deviceSerial) {
        addSubscribe(dataManager.addEzDevicePermission(accessToken, tel, toTel, deviceSerial).compose(RxUtil.<BaseBean>rxSchedulerHelper()).subscribe(new Consumer<BaseBean>() {
            @Override
            public void accept(BaseBean bean) throws Exception {
                mView.viewAddEzDevicePermissionResult(bean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LogUtil.e(throwable.getMessage());
            }
        }));
    }

    @Override
    public void delEzDevicePermission(String accessToken, String tel, String toTel, String statement) {
        addSubscribe(dataManager.deleteEzDevicePermission(accessToken, tel, toTel, statement).compose(RxUtil.<BaseBean>rxSchedulerHelper()).subscribe(new Consumer<BaseBean>() {
            @Override
            public void accept(BaseBean bean) throws Exception {
                mView.viewDelEzDevicePermissionResult(bean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LogUtil.e(throwable.getMessage());
            }
        }));
    }


    @Override
    public void getEzDeviceList(String accessToken, String tel, String toTel) {
        addSubscribe(dataManager.getEzDeviceAuthorzationList(accessToken, tel, toTel).compose(RxUtil.<BaseBean<List<EzDeviceAuthorizationListBean>>>rxSchedulerHelper()).subscribe(new Consumer<BaseBean<List<EzDeviceAuthorizationListBean>>>() {
            @Override
            public void accept(BaseBean<List<EzDeviceAuthorizationListBean>> bean) throws Exception {
                mView.viewEzDeviceListResult(bean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LogUtil.e(throwable.getMessage());
            }
        }));
    }


}
